/*
 * Copyright (c) 2010 Curtis Boyden.  All rights reserved. 
 * This software was developed by Curtis Boyden and is provided under the terms 
 * of the GNU Lesser General Public License, Version 2.1. You may not use 
 * this file except in compliance with the license. If you need a copy of the license, 
 * please go to http://www.gnu.org/licenses/lgpl-2.1.txt. The Original Code is ejMud
 * (Ewerp Java MUD).  The Initial Developer is Curtis Boyden.
 *
 * Software distributed under the GNU Lesser Public License is distributed on an "AS IS" 
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or  implied. Please refer to 
 * the license for the specific language governing your rights and limitations.
*/

package com.ewerp.mud.entities;

import java.io.Serializable;

public class ObjectId implements IObjectId, Serializable {
    private static final long serialVersionUID = 1L;
    
    protected long serial;
    protected String objectClass;
    protected IEntity object;
    
    public ObjectId() {
        serial = -1;
    }
    
    public ObjectId(long id) {
        this.serial = id;
    }
    
    public ObjectId(String hexString) {
        serial = Long.parseLong(hexString, 16);
    }
    
    @Override
    public IEntity getObject() {
        return object;
    }
    
    public void setObject(IEntity object) {
        this.object = object;
    }
    
    // Hibernate only
    public long getSerial() {
        return serial;
    }

    // Hibernate only
    public void setSerial(long serial) {
        this.serial = serial;
    }
    
    // Hibernate only
    public String getObjectClass() {
        return objectClass == null ? null : objectClass;
    }
    
    // Hibernate only
    public void setObjectClass(String objectClass) {
        this.objectClass = objectClass; 
    }
    
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null || !(obj instanceof ObjectId)) {
            return false;
        }
        
        ObjectId objId = (ObjectId) obj;
        
        if(objId.getSerial() != getSerial()) {
            return false;
        }
        
        // A null ObjectClass is a wild card for matches
        if(objId.getObjectClass() != null && getObjectClass() != null
           && !objId.getObjectClass().equals(getObjectClass())) {
            return false;
        }
        
        return true;
    }

    @Override
    public int hashCode() {
        // If the id is Int.MAX or less, use it
        if(serial <= Integer.MAX_VALUE) {
            return (int) serial;
        }
        
        // Use the negative value of the id less Int.MAX
        return (int) -(serial - Integer.MAX_VALUE);
    }

    @Override
    public String toString() {
        return Long.toHexString(serial);
    }

}
